#include "SceneGraph.h"

CSceneGraph::CSceneGraph()
{}

CSceneGraph::~CSceneGraph()
	{		
		for (std::vector<MeshPtr>::iterator ite = m_vecMeshes.begin();
			ite != m_vecMeshes.end(); ++ite)
			(*ite).reset();
		for (std::vector<ShaderPtr>::iterator ite = m_vecShaders.begin();
			ite != m_vecShaders.end(); ++ite)
			(*ite).reset();
		for (std::vector<CameraPtr>::iterator ite = m_vecCameras.begin();
			ite != m_vecCameras.end(); ++ite)
			(*ite).reset();
		for (std::vector<ObjectPtr>::iterator ite = m_vecObjects.begin();
			ite != m_vecObjects.end(); ++ite)
			(*ite).reset();
	}



void CSceneGraph::render()
{
	// render camera mesh?
	for (std::vector<CameraPtr>::iterator iteCam = m_vecCameras.begin();
		iteCam != m_vecCameras.end(); ++iteCam)
	{
		(*iteCam)->activateCamera();
		for (std::vector<ObjectPtr>::iterator iteObj = m_vecObjects.begin();
			iteObj != m_vecObjects.end(); ++iteObj)
		{
			std::vector<MeshPtr>::iterator iteMesh = find_if(m_vecMeshes.begin(), m_vecMeshes.end(), std::bind2nd(MeshIdentity(), (*iteObj)->getMeshID()));

			if (iteMesh != m_vecMeshes.end())
			{
				if ((*iteObj)->getShaderID() == 0)
				{
					(*iteMesh)->render();
				}
				else
				{
					std::vector<ShaderPtr>::iterator iteShader =  find_if(m_vecShaders.begin(), m_vecShaders.end(), std::bind2nd(ShaderIdentity(), (*iteObj)->getShaderID()));

					if (iteShader != m_vecShaders.end())
					{
						(*iteShader)->render((*iteMesh));
					}
				}
			}
		}
	}
}


void CSceneGraph::addMesh(MeshPtr pMesh)
{
	m_vecMeshes.push_back(pMesh);
}

void CSceneGraph::addShader(ShaderPtr pShader)
{
	m_vecShaders.push_back(pShader);
}

void CSceneGraph::addCamera(CameraPtr pCamera)
{
	m_vecCameras.push_back(pCamera);
}

void CSceneGraph::addObject(ObjectPtr pObject)
{
	m_vecObjects.push_back(pObject);
}